package com.cloudbroker.openapi.mobile.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 模拟数据获取工具
 * @author shenmy14613
 */
public class SimDataUtil {
	private static final Logger logger = LoggerFactory.getLogger(SimDataUtil.class);

	public static void main(String[] args) {
		//Map<String, Object> map = getMapList("func_test");
		//System.out.println(map.toString());

		System.out.println(System.getProperty("os.name"));
		System.out.println(System.getProperty("user.dir"));
	}

	/**
	 * 获取不含列表的数据
	 * @param funcName
	 * @return
	 */
	public static Map<String, Object> getMap(String funcName) {
		String jsonStr = readFile(funcName);
		JSONObject jsonObj = JSON.parseObject(jsonStr);
		HashMap<String, Object> map = new HashMap<String, Object>();
		for (Entry<String, Object> json : jsonObj.entrySet()) {
			map.put(json.getKey(), String.valueOf(json.getValue()));
		}
		return map;
	}

	/**
	 * 获取含有列表的数据
	 * @param funcName
	 * @return
	 */
	public static Map<String, Object> getMapList(String funcName) {
		String jsonStr = readFile(funcName);
		JSONObject jsonObj = JSON.parseObject(jsonStr);
		Map<String, Object> listMap = new HashMap<>();
		for (Entry<String, Object> entry : jsonObj.entrySet()) {
			try {
				List<String> valueJson = JSON.parseArray(
						JSON.toJSONString(entry.getValue()), String.class);
				List<Map<String, String>> valueList = new ArrayList<>();
				for (String string : valueJson) {
					JSONObject json = JSON.parseObject(string);
					HashMap<String, String> map = new HashMap<String, String>();
					for (Entry<String, Object> j : json.entrySet()) {
						map.put(j.getKey(), String.valueOf(j.getValue()));
					}
					valueList.add(map);
				}
				listMap.put(entry.getKey(), valueList);
			} catch (Exception e) {
				listMap.put(entry.getKey(), String.valueOf(entry.getValue()));
			}
		}
		return listMap;
	}

	private static String readFile(String funcName) {
		// 从JSon文件读取数据
		StringBuffer stringBuffer = new StringBuffer();
		String osName = System.getProperty("os.name").toLowerCase();
		String line = null;
		try {
			logger.info("==================\n" + System.getProperty("user.dir")+"/testdata" );
			ClassLoader classLoader = SimDataUtil.class.getClassLoader();
			File file;
			if(osName.startsWith("win")){
				file = new File(classLoader.getResource("testdata").getFile()
						+ File.separator + funcName + ".json");
			}else{
				file = new File(System.getProperty("user.dir")+File.separator+"testdata"+File.separator + funcName + ".json");
			}

			BufferedReader br = new BufferedReader(new FileReader(file));
			while ((line = br.readLine()) != null) {
				stringBuffer.append(line);
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return stringBuffer.toString();
	}

}
